package com.zccx.eth.util;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * 业务信息获取类
 * @author Jian
 * @description
 * @created 2018/4/2 14:01.
 */
public class BisMessage {
    private static final String propertiesFile = "properties";

    private static ResourceBundle resources = null;

    /**
     * 获取配置信息
     * @param property
     * @return
     */
    public static String getValue(int property) {
        String val = "";
        try {
            if (resources == null) {
                initResources();
            }
            val = resources.getString(property+"");
        } catch (Exception e) {
            // ignore
        }
        return val;
    }

    /**
     * 获取配置信息
     * @param property
     * @param obj
     * @return
     */
    public static String getValue(int property, Object[] obj) {
        String val = "";
        try {
            if (resources == null) {
                initResources();
            }
            val = resources.getString(property+"");
            val = MessageFormat.format(val, obj);
        } catch (Exception e) {
            // ignore
        }
        return val;

    }

    /**
     * 初始化配置文件
     */
    private static void initResources() {
        try {
            resources = ResourceBundle.getBundle(propertiesFile, Locale.CHINESE);
        } catch (MissingResourceException e) {
            throw new RuntimeException(e);
        }
    }



    /**
     * 根据配置文件获取指定key值
     * @param propertiesFile 属性文件
     * @param property 属性名
     * @return
     */
    public static String getValue(String propertiesFile,
                                            String property) {
        String val = "";
        ResourceBundle resources1 = null;
        try {
            resources1 = ResourceBundle.getBundle(propertiesFile);
            val = resources1.getString(property);
        }
        catch (MissingResourceException e) {
            // ignore
        }
        return val;
    }


    /**
     * test
     * @param args
     */
    public static void main(String[] args) {
        String value = getValue(103);
        System.out.println(value);
    }

}
